import 'package:app/core/net/config/global.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';

// https://pub.flutter-io.cn/packages/get_storage
/// 本地存储
class SpUtil {
  StorageController controller;
  SpUtil._privateConstructor() {
    controller = Get.put(StorageController());
  }

  static final SpUtil _instance = SpUtil._privateConstructor();

  static SpUtil get instance {
    return _instance;
  }

  // StorageController controller = Get.find<StorageController>();
}

class StorageController extends GetxController {
  final box = GetStorage();
  bool get isDark => box.read('darkmode') ?? false;
  ThemeData get theme => isDark ? ThemeData.dark() : ThemeData.light();
  void changeTheme(bool val) => box.write('darkmode', val);

  //token
  String get token => box.read(Global.TOKEN_KEY);
  setToken(String token) => box.write(Global.TOKEN_KEY, token);

  // 用户信息
  String get userInfo => box.read(Global.USER_INFO_KEY);
  setUserInfo(String info) => box.write(Global.USER_INFO_KEY, info);
}
